<script type="text/javascript">
function myAjax(type,url,params,dataType,callback,async){
	var xhr = null;
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else{
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(type=="get"){
		if(params && params !=""){
			url +="?"+params; 
		}
		xhr.open(type,url,async);
	}else{
		xhr.open(type,url,async);
	}
	if(type=="get"){
		xhr.send(null);
	}else{
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send(params);
	}

	if(async){
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4 && xhr.status==200){
				var result = null;
				if(dataType=="json"){
					result = JSON.parse(xhr.responseText);
				}else if(dateType=="xml"){
					result = xhr.responseXML;
				}else{
					result = xhr.responseText;
				}
				if(callback){
					callback(result);
				}

			}
		};
	}else{
		if(xhr.readyState==4 && xhr.status==200){
			var result = null;
			if(dataType=="json"){
				result = JSON.parse(xhr.responseText);
			}else if(dateType=="xml"){
				result = xhr.responseXML;
			}else{
				result = xhr.responseText;
			}
			if(callback){
				callback(result);
			}

		}
	} 

}

function myAjax2(obj){
	var defaults = {
			type:"get",
			url:"#",
			dataType:"json",
			async:"true",
			data:{},
			success: function(result){
				console.log(result);
			}
	};

	for(var key in obj){
		defaults[key] = obj[key];
	}

	var xhr = null;
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else{
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}

	var params = "";
	for(var attr in defaults.data){
		params += attr+"="+defaults.data[attr]+"&";
	}
	if(params){
		params = params.substring(0, params.length-1);
	}


	if(defaults.type=="get"){
		defaults.url += "?"+params;
	}
	xhr.open(defaults.type,defaults.url,defaults.async);

	if(defaults.type=="get"){
		xhr.send(null);
	}else if(defaults.type=="post"){
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send(params);
	}

	if(defaults.async){
		xhr.onreadystatechange = function(){
			if(xhr.status==200 && xhr.readyState==4){
				var result = null;
				if(defaults.dataType=="json"){
					result = JSON.parse(xhr.responseText);
				}else if(defaults.dataType=="xml"){
					result = JSON.parse(xhr.responseXML);
				}else{
					result = xhr.responseText;
				}
				success(result);
			}
		}
	}else{
		if(xhr.status==200 && xhr.readyState==4){
			var result = null;
			if(defaults.dataType=="json"){
				result = JSON.parse(xhr.responseText);
			}else if(defaults.dataType=="xml"){
				result = JSON.parse(xhr.responseXML);
			}else{
				result = xhr.responseText;
			}
			success(result);
		}
	}
}
</script>
